#Helper Functions

import re

from models import Sub_Components
from models import Page_Profile
from models import View_Summary
from models import View_Total_Size
from datetime import datetime
import django.core
from django.core.exceptions import ValidationError


#Find element in array
#def find(l, elem):
#	for row, i in enumerate(l):
#		try:
#    	column = i.index(elem)
#		except ValueError:
#		return l[row][1]
#	return -1

# Add Page Profile
def addPageProfile(data):
	e1 = Page_Profile()
	e1.url = data
	e1.timestamp = datetime.now()
	e1.save()
	i = Page_Profile.objects.filter(url=data)
	return int(i[0].url_id) #returns URL ID
	
# Add Sub Component 
def addSubComponents(data):
	contentType = re.split('/',data['content-type']) # WW
	e1 = Sub_Components()
	e1.obj_type = contentType[0].lower()
	e1.url_id = data['url_id'] # to be constructed
	e1.sub_url = data['sub_url']  # to be constructed
	e1.size = data['content-length']
	e1.sub_type = contentType[1].lower()
	e1.save()	

#View_summary 
#def viewSummary():
#	for e1 in View_Summary.objects.filter(url_id=11):
#		print(e1.url_id)
#    print(e1.object_type)
#    print(e1.object_size)

